package com.example.lambda.lambda_demo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: lambad的常见表达式
 * @author: WangBlue
 * @create: 2024-03-08 22:52
 **/
public class Demo {
    public static void main(String[] args) {
//        1.  使用Lambda表达式进行集合遍历
//         要遍历的数组
//        List<String> list = Arrays.asList("张三", "李四", "王五");
//        for (String fruit : list) {
//            System.out.println(fruit);
//        }
//        List<String> strings = new ArrayList<>();
//        list.forEach(s -> {
//            strings.add(s);
//            System.out.println(s);
//        });
//        System.out.println(strings);

//         2.  遍历map
//        Map<String, Integer> map = new HashMap<>();
//        map.put("张三", 30);
//        map.put("李四", 25);
//        map.put("王五", 35);
//        map.forEach((name, age) -> {
//            System.out.println("name: " + name + ", age: " + age);
//        });


//         3.   使用Lambda表达式进行排序
//        List<String> list = Arrays.asList("apple", "cherry", "orange", "banana" );
//        // 使用lambda表达式和List的sort方法进行排序
//        list.sort((s1, s2) -> s1.compareTo(s2));
//        // 打印排序后的列表
//        list.forEach(System.out::println);
//        // 按照单词的首字母进行排序, 匿名内部类方式
//        Collections.sort(list, new Comparator<String>() {
//            @Override
//            public int compare(String s1, String s2) {
//                return Character.compare(s1.charAt(0), s2.charAt(0));
//            }
//        });
//        System.out.println(list);

////        4. 使用Lambda表达式进行过滤
//        List<String> list = Arrays.asList("apple", "banana", "orange");
//        List<String> filteredList = new ArrayList<String>();
//        for (String fruit : list) {
//            if (fruit.startsWith("a")) {
//                filteredList.add(fruit);
//            }
//        }
//
//
//        List<String> filterList =  list.stream().filter(fruit -> {
//            return fruit.startsWith("a");
//        }).collect(Collectors.toList());
//
//        List<String> fil = list.stream()
//                .filter(fruit -> fruit.startsWith("a"))
//                .collect(Collectors.toList());
//        System.out.println(fil);
//    }


////                4.  使用Lambda表达式进行映射
//        List<String> list = Arrays.asList("apple", "banana", "orange");
//        List<Integer> lengths = new ArrayList<Integer>();
//        for (String fruit : list) {
//            lengths.add(fruit.length());
//        }

//        List<String> lists = Arrays.asList("apple", "banana", "orange");
//        List<Integer> lengthss = lists.stream().map(fruit -> fruit.length())
//                .collect(Collectors.toList());
//        System.out.println("lengthss = " + lengthss);

//        List<Integer> len = list.stream().map(length-> {
//            return length.length();
//        }).collect(Collectors.toList());
//        System.out.println("len = " + len);


//    //        5.  使用Lambda表达式进行归约
//        List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5);
//        int dif = list2.stream().reduce(0, (a, b) -> a-b);
//        System.out.println("dif = " + dif);

        //        6.  使用Lambda表达式进行分组
//        List<String> list = Arrays.asList("apple", "banana", "orange");
//        Map<Integer, List<String>> grouped = new HashMap<Integer, List<String>>();
//        for (String fruit : list) {
//            int length = fruit.length();
//            if (!grouped.containsKey(length)) {
//                grouped.put(length, new ArrayList<String>());
//            }
//            grouped.get(length).add(fruit);
//        }
//        System.out.println("grouped = " + grouped);



//        Map<Integer, List<String>> groupeds = lists.stream().collect(Collectors.groupingBy(fruit -> fruit.length()));
//        System.out.println("groupeds = " + groupeds);



        //8.  使用Lambda表达式进行线程的创建
//        Thread thread = new Thread(new Runnable() {
//            public void run() {
//                System.out.println("Thread is running.");
//            }
//        });
//        thread.start();
//
//        Thread helloThread = new Thread(() -> System.out.println("hello thread"));
//        helloThread.start();

        Thread threads = new Thread(() -> System.out.println("Thread is running."));
        threads.start();

    }

}
